#!/bin/bash
for i in $@; do
    dos2unix $i
    ENCODING="`file -b --mime-encoding $i`"
    if [ "$ENCODING" = "iso-8859-1" ] || [ "$ENCODING" = "us-ascii" ]; then # 注意操作符两边空格
        iconv -f gb2312 -t utf-8 $i > tmp;mv tmp $i
        echo "iconv:    converting file $i encoding gb2312 to utf-8..."
    else
        if [ "$ENCODING" != "utf-8" ]; then
            echo "iconv:    unknow file $i encoding type $ENCODING"
            #exit
        fi
    fi

done

for i in $@; do
    ENCODING="`file -b $i`"
    UTF8="UTF-8"
    if [[ $ENCODING == *$UTF8* ]] && [[ $ENCODING != *BOM* ]] 
    then
        sed -i '1s/^/\xef\xbb\xbf/' $i
        echo "sed:    add $i type $ENCODING with BOM"
    fi
done
